//
//  Player.m
//  CandyCrushClone
//
//  Created by Petar on 1/31/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "Player.h"
#import "RWTCookie.h"

static const NSInteger MATCHED_4_COOKIES_MULTIPLIER = 1;
static const NSInteger MATCHED_5_COOKIES_MULTIPLIER = 3;

@implementation Player
{
    NSInteger _matched4CookiesCounter;
    NSInteger _matched5CookiesCounter;
}


- (instancetype)initWithHeroType:(NSInteger)heroType
{
    self = [super init];
    if (self) {
        
        _playerId = 0;
        _cookieType1Counter = 0;
        _cookieType2Counter = 0;
        _cookieType3Counter = 0;
        _cookieType4Counter = 0;
        _cookieType5Counter = 0;
        _cookieType6Counter = 0;
        _matched4CookiesCounter = 0;
        _matched5CookiesCounter = 0;
        _hasMatched4Cookies = NO;
        _hasMatched5Cookies = NO;
        _playerInfoFromServer = [NSArray new];
        _hero = [[Hero alloc] initWithType:heroType];
        
    }
    return self;
}


-(void)calculateScoreForChains:(NSSet *)chains
{
    
    for (RWTChain *chain in chains) {
        
        [self calculateScoreForChain:chain];
        [_hero calculateScoreForChain:chain];
        
        if ( [chain.cookies count] == 4 ) {
            
            _matched4CookiesCounter += 1;
            _hasMatched4Cookies = YES;
            
        } else if ( [chain.cookies count] == 5 ) {
            
            _matched5CookiesCounter += 1;
            _hasMatched5Cookies = YES;
            
        }
        
    }
    
    [self addComboCookies];
    [self normalizeCountersForConditiones];
    
}


/*
 * Updates counter values for cookies from given chain
 */
-(void)calculateScoreForChain:(RWTChain *)chain
{
    
    int value = 1;
    
    for (RWTCookie *cookie in chain.cookies) {
        
        switch ( cookie.cookieType ) {
            case 1:
                _cookieType1Counter += value;
                break;
            case 2:
                _cookieType2Counter += value;
                break;
            case 3:
                _cookieType3Counter += value;
                break;
            case 4:
                _cookieType4Counter += value;
                break;
            case 5:
                _cookieType5Counter += value;
                break;
            case 6:
                _cookieType6Counter += value;
                break;
                
            default:
                break;
        }
        
    }
    
}


- (void)addComboCookies
{
    
    NSInteger matched4ComboCookies = _matched4CookiesCounter * MATCHED_4_COOKIES_MULTIPLIER;
    NSInteger matched5ComboCookies = _matched5CookiesCounter * MATCHED_5_COOKIES_MULTIPLIER;
    
    NSInteger comboCookiesToBeAdded = matched4ComboCookies + matched5ComboCookies;
    
    // reset matched combo counters
    _matched4CookiesCounter = 0;
    _matched5CookiesCounter = 0;
    
    //update counters
    _cookieType1Counter += comboCookiesToBeAdded;
    _cookieType2Counter += comboCookiesToBeAdded;
    _cookieType3Counter += comboCookiesToBeAdded;
    _cookieType4Counter += comboCookiesToBeAdded;
    _cookieType5Counter += comboCookiesToBeAdded;
    _cookieType6Counter += comboCookiesToBeAdded;
    
    [_hero addComboCookies:comboCookiesToBeAdded];
    
}


- (void)normalizeCountersForConditiones
{
    
    NSInteger cookieType1Target = [_gameWinConditions[@"1"] integerValue];
    NSInteger cookieType2Target = [_gameWinConditions[@"2"] integerValue];
    NSInteger cookieType3Target = [_gameWinConditions[@"3"] integerValue];
    NSInteger cookieType4Target = [_gameWinConditions[@"4"] integerValue];
    
    if ( _cookieType1Counter > cookieType1Target ) {
        _cookieType1Counter = cookieType1Target;
    }
    
    if ( _cookieType2Counter > cookieType2Target ) {
        _cookieType2Counter = cookieType2Target;
    }
    
    if ( _cookieType3Counter > cookieType3Target ) {
        _cookieType3Counter = cookieType3Target;
    }
    
    if ( _cookieType4Counter > cookieType4Target ) {
        _cookieType4Counter = cookieType4Target;
    }
    
}


/*
 * Reset all counters to 0
 */
-(void)resetCounters
{
    
    _cookieType1Counter = 0;
    _cookieType2Counter = 0;
    _cookieType3Counter = 0;
    _cookieType4Counter = 0;
    _cookieType5Counter = 0;
    _cookieType6Counter = 0;
    
    // Reset Hero Counters
    [_hero resetCounters];
}


/*
 * Update the counters for cookies with server values to prevent manipulation!
 */
-(void)setCounterValuesFromServer
{
    
    // at index 0 is playerID
    // at indexes from 1 to 6 are values for the cookies counters
    for (int i = 1; i < [_playerInfoFromServer count]; i++) {
        
        NSInteger counterValue = [_playerInfoFromServer[i] integerValue];
        
        switch ( i ) {
            case 1:
                _cookieType1Counter = counterValue;
                break;
            case 2:
                _cookieType2Counter = counterValue;
                break;
            case 3:
                _cookieType3Counter = counterValue;
                break;
            case 4:
                _cookieType4Counter = counterValue;
                break;
            case 5:
                _cookieType5Counter = counterValue;
                break;
            case 6:
                _cookieType6Counter = counterValue;
                break;
                
            default:
                break;
        }
        
    }
    
    [_hero setCounterValuesFromServer];
    
}


- (void)performHeroSkillWithOtherPlayer:(Player *)otherPlayer
{
    
    [self useHeroSkill:_hero.skillsOnMyself];
    [otherPlayer useHeroSkill:_hero.skillsOnOtherPlayer];
    
    [_hero decreaseCookieCounters];

}


- (void)useHeroSkill:(NSDictionary *)heroSkill
{
    
    // at indexes from 1 to 6 are values for the cookies counters
    for (int i = 1; i <= [heroSkill count]; i++) {
        
        NSString *counterKey = [NSString stringWithFormat:@"%d", i];
        NSInteger skillValue = [heroSkill[counterKey] integerValue];
        
        // Updates Player and Hero counters by Hero Skills
        switch ( i ) {
                
            case 1:
                _cookieType1Counter += skillValue;
                _hero.cookieType1Counter += skillValue;
                break;
                
            case 2:
                _cookieType2Counter += skillValue;
                _hero.cookieType2Counter += skillValue;
                break;
                
            case 3:
                _cookieType3Counter += skillValue;
                _hero.cookieType3Counter += skillValue;
                break;
                
            case 4:
                _cookieType4Counter += skillValue;
                _hero.cookieType4Counter += skillValue;
                break;
                
            case 5:
                _cookieType5Counter += skillValue;
                _hero.cookieType5Counter += skillValue;
                break;
                
            case 6:
                _cookieType6Counter += skillValue;
                _hero.cookieType6Counter += skillValue;
                break;
                
            default:
                break;
        }
        
    }
    
}


@end
